package com.hinotoyk.testsystem.uitls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hinotoyk.testsystem.entity.*;
import com.hinotoyk.testsystem.mappers.SGQuestionMapper;
import com.hinotoyk.testsystem.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/* 试卷JSON格式
 * {
 *      "score": 总分,
 *      "stime": 开始时间,
 *      "ttime": 时长,
 *      "sg":{
 *          "id":[ 题目id ],
 *          "score": 每题分数
 *      },
 *      "pl":{ ... 和 sg 一样},
 *      "tof":{ ... 和 sg 一样},
 *      "essay":{ ... 和 sg 一样},
 *      "fill":{ ... 和 sg 一样}
 * }
 */
@Service
public class TestPagerJson {
    private List<Integer> sgList = new ArrayList<Integer>();
    private List<Integer> plList = new ArrayList<Integer>();
    private List<Integer> tofList = new ArrayList<Integer>();
    private List<Integer> fillList = new ArrayList<Integer>();
    private List<Integer> essayList = new ArrayList<Integer>();

    private TestPaperSave save = new TestPaperSave();

    private int totalScore = 0;
    private int sgScore = 0;
    private int plScore = 0;
    private int tofScore = 0;
    private int fillScore = 0;
    private int essayScore = 0;

    private String startTime = "";
    private int testTime = 0;

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public int getTestTime() {
        return testTime;
    }

    public void setTestTime(int testTime) {
        this.testTime = testTime;
    }

    public List<Integer> getListByQuestionType(int type){
        if(type == QuestionType.SGQuestion.getType()){
            return getSgList();
        }else if(type == QuestionType.FillingQuestion.getType()){
            return getFillList();
        }else if(type == QuestionType.EssayQuestion.getType()){
            return getEssayList();
        }else if(type == QuestionType.PLQuestion.getType()){
            return getPlList();
        }else if(type == QuestionType.TOFQuestion.getType()){
            return getTofList();
        }

        return null;
    }

    public int getScoreByQuestionType(int type){
        if(type == QuestionType.SGQuestion.getType()){
            return getSgScore();
        }else if(type == QuestionType.FillingQuestion.getType()){
            return getFillScore();
        }else if(type == QuestionType.EssayQuestion.getType()){
            return getEssayScore();
        }else if(type == QuestionType.PLQuestion.getType()){
            return getPlScore();
        }else if(type == QuestionType.TOFQuestion.getType()){
            return getTofScore();
        }

        return 0;
    }

    public TestPaperSave getSave() {
        return save;
    }

    public void setSave(TestPaperSave save) {
        this.save = save;
    }

    public List<Integer> getSgList() {
        return sgList;
    }

    public List<Integer> getPlList() {
        return plList;
    }

    public List<Integer> getTofList() {
        return tofList;
    }

    public List<Integer> getFillList() {
        return fillList;
    }

    public List<Integer> getEssayList() {
        return essayList;
    }

    public void setSgList(List<Integer> sgList) {
        if(sgList != null)
            this.sgList = sgList;
    }

    public void setPlList(List<Integer> plList) {
        if(plList != null)
            this.plList = plList;
    }

    public void setTofList(List<Integer> tofList) {
        if(tofList != null)
            this.tofList = tofList;
    }

    public void setFillList(List<Integer> fillList) {
        if(fillList != null)
            this.fillList = fillList;
    }

    public void setEssayList(List<Integer> essayList) {
        if(essayList != null)
            this.essayList = essayList;
    }

    public int getTotalScore() {
        return totalScore;
    }

    public int getSgScore() {
        return sgScore;
    }

    public int getPlScore() {
        return plScore;
    }

    public int getTofScore() {
        return tofScore;
    }

    public int getFillScore() {
        return fillScore;
    }

    public int getEssayScore() {
        return essayScore;
    }

    public void setTotalScore(int totalScore) {
        this.totalScore = totalScore;
    }

    public void setSgScore(int sgScore) {
        this.sgScore = sgScore;
    }

    public void setPlScore(int plScore) {
        this.plScore = plScore;
    }

    public void setTofScore(int tofScore) {
        this.tofScore = tofScore;
    }

    public void setFillScore(int fillScore) {
        this.fillScore = fillScore;
    }

    public void setEssayScore(int essayScore) {
        this.essayScore = essayScore;
    }

    public void addSGQuestion(int id){
        sgList.add(id);
    }

    public void addPLQuestion(int id){
        plList.add(id);
    }


    public void addTOFQuestion(int id){
        tofList.add(id);
    }

    public void addFillingQuestion(int id){
        fillList.add(id);
    }

    public void addEssayQuestion(int id){
        essayList.add(id);
    }

    public void ReCalculateTotalScore(){
        totalScore = sgScore * sgList.size() + plScore * plList.size() + tofScore * tofList.size() + essayScore * essayList.size() + fillScore * fillList.size();
    }

    public static TestPagerJson MakeByJSONString(String str){
        TestPagerJson json = new TestPagerJson();
        JSONObject parser = JSON.parseObject(str);

        json.setTotalScore(parser.getIntValue("score"));
        json.setStartTime(parser.getString("stime"));
        json.setTestTime(parser.getIntValue("ttime"));
        json.setSgScore(parser.getJSONObject("sg").getIntValue("score"));
        json.sgList = parser.getJSONObject("sg").getJSONArray("id").toJavaList(Integer.TYPE);

        json.setPlScore(parser.getJSONObject("pl").getIntValue("score"));
        json.plList = parser.getJSONObject("pl").getJSONArray("id").toJavaList(Integer.TYPE);

        json.setTofScore(parser.getJSONObject("tof").getIntValue("score"));
        json.tofList = parser.getJSONObject("tof").getJSONArray("id").toJavaList(Integer.TYPE);

        json.setEssayScore(parser.getJSONObject("essay").getIntValue("score"));
        json.essayList = parser.getJSONObject("essay").getJSONArray("id").toJavaList(Integer.TYPE);

        json.setFillScore(parser.getJSONObject("fill").getIntValue("score"));
        json.fillList = parser.getJSONObject("fill").getJSONArray("id").toJavaList(Integer.TYPE);

        return json;
    }

    public static TestPagerJson RandomByJSONString(String str){
        TestPagerJson json = new TestPagerJson();
        JSONObject parser = JSON.parseObject(str);

        JSONObject helper = null;
        List<Integer> list = null;

        json.setTotalScore(parser.getIntValue("score"));
        json.setStartTime(parser.getString("stime"));
        json.setTestTime(parser.getIntValue("ttime"));

        helper = parser.getJSONObject("sg");
        list = helper.getJSONArray("id").toJavaList(Integer.TYPE);
        if(list != null) {
            Collections.shuffle(list);
            json.setSgList(list);
        }
        json.setSgScore(helper.getIntValue("score"));

        helper = parser.getJSONObject("pl");
        list = helper.getJSONArray("id").toJavaList(Integer.TYPE);
        if(list != null) {
            Collections.shuffle(list);
            json.setPlList(list);
        }
        json.setPlScore(helper.getIntValue("score"));

        helper = parser.getJSONObject("tof");
        list = helper.getJSONArray("id").toJavaList(Integer.TYPE);
        if(list != null) {
            Collections.shuffle(list);
            json.setTofList(list);
        }
        json.setTofScore(helper.getIntValue("score"));

        helper = parser.getJSONObject("fill");
        list = helper.getJSONArray("id").toJavaList(Integer.TYPE);
        if(list != null) {
            Collections.shuffle(list);
            json.setFillList(list);
        }
        json.setFillScore(helper.getIntValue("score"));

        helper = parser.getJSONObject("essay");
        list = helper.getJSONArray("id").toJavaList(Integer.TYPE);
        if(list != null) {
            Collections.shuffle(list);
            json.setEssayList(list);
        }
        json.setEssayScore(helper.getIntValue("score"));
        return json;
    }

    static String MakeQuesionJsonStr(List<Integer> list, int score){
        String str = new String();
        str += "id:[";
        for(int i = 0; i < list.size(); ++i){
            str += list.get(i);
            if(i < list.size()-1)
                str += ',';
        }
        str += ("],score:" + score);

        return str;
    }

    static JSONObject MakeQuesionJsonObject(List<Integer> list, int score){
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();

        for(int i = 0; i < list.size(); ++i){
            array.add(i, list.get(i));
        }
        obj.put("id", array);
        obj.put("score", score);

        return obj;
    }

    public JSONObject toJSONObject(){
        JSONObject obj = new JSONObject();
        obj.put("score", totalScore);
        obj.put("stime", startTime);
        obj.put("ttime", testTime);
        obj.put("sg", MakeQuesionJsonObject(sgList, sgScore));
        obj.put("pl", MakeQuesionJsonObject(plList, plScore));
        obj.put("fill", MakeQuesionJsonObject(fillList, fillScore));
        obj.put("tof", MakeQuesionJsonObject(tofList, tofScore));
        obj.put("essay", MakeQuesionJsonObject(essayList, essayScore));
        return obj;
    }

    public String toJSONString(){
        return toJSONObject().toJSONString();
    }

    @Override
    public String toString() {
        return toJSONString();
    }
}
